﻿/**********************************天使物流*******************************************
* 功能描述:基本信息-货主
* 作者:李周蓬
* 时间:
* 
**********************************天使物流*******************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.BaseInfo.Owners;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.BaseInfo.Owners
{
    public class OwnerRepository : DapperDbContextRepository, IOwnerRepository
    {
        public OwnerRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
            
        }
        /// <summary>
        /// 显示货主
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <param name="time"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<OwnerDto>> GetShowOwner(string name,string phone,DateTime? time, int pageindex, int pagesize)
        {
            PageResponse<OwnerDto> response = new PageResponse<OwnerDto>();
            var sql = $"select a.*,b.OutsourceName from Owner a join Outsource b on a.OutsourceId=b.Id where a.IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(phone))
            {
                sql += $" and a.OwnerPhone='{phone}'";
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.OwnerName like '%" + name + "%'";
            }
            if (time!=null)
            {
                sql += $" and a.DriverCardTime='{time}'";
            }
            var list = await this.QueryAsync<OwnerDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<OwnerDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除货主
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelOwner(int id)
        {
            var sql = $"update Owner set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除货主
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsOwner(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Owner set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="OwnerDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateOwner(OwnerDto OwnerDto)
        {
            var sql = $"update Owner set OwnerName='{OwnerDto.OwnerName}',OwnerPhone='{OwnerDto.OwnerPhone}',OwnerAddr='{OwnerDto.OwnerAddr}',DriverCardTime='{OwnerDto.DriverCardTime.ToString("yyyy/MM/dd")}',Remark='{OwnerDto.Remark}',OutsourceId='{OwnerDto.OutsourceId}' where Id='{OwnerDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加货主
        /// </summary>
        /// <param name="OwnerDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddOwner(OwnerDto OwnerDto)
        {
            var sql = $"insert into Owner values('{OwnerDto.OwnerNumber}','{OwnerDto.OwnerName}','{OwnerDto.OwnerPhone}','{OwnerDto.OwnerAddr}','{OwnerDto.DriverCardTime.ToString("yyyy/MM/dd")}','{OwnerDto.CardImg}','{OwnerDto.Remark}',0,'{OwnerDto.OutsourceId}')";
             var list= await ExecuteAsync
                (sql, OwnerDto);
            return list;
        }
        /// <summary>
        /// 绑定外协单位
        /// </summary>
        /// <returns></returns>
        public async Task<List<OutsourceDto>> GetShowOutsource()
        {
            PageResponse<OutsourceDto> response = new PageResponse<OutsourceDto>();
            var sql = $"select * from Outsource where IsDelete=0  ";
            response.PageData = await this.QueryAsync<OutsourceDto>(sql, null);
            return response.PageData;
        }
    }
}
